"""Tests the top window in example/Project1
"""

from watsup.winGuiAuto import findControl,clickButton,getEditText,activateMenuItem, \
                       getComboboxItems, findControls, dumpWindow,\
                       getTopMenu
from watsup.performance import PerformanceCheck                        
from watsup.utils import dumpHwnd                        
from watsup.launcher import launchApp,terminateApp
from watsup.PControl import PLaunchApp, MenuItem

import win32gui

# Author     : Tim Couper - timc@tizmoi.net
# Date       : 22 July 2004
# Version    : 1.0 
# Copyright  : Copyright TAC Software Ltd, under Python-style licence.
#              Provided as-is, with no warranty.

#-------------------------------------------------------------------------------

#from unittest import TestCase
from watsup.timedunittest import TimedTestCase as TestCase

#-------------------------------------------------------------------------------
APP='../examples/unittests/example/example1'

import unittest 

class ExampleCase8Again(TestCase):
    def setUp(self): 
        # launch the chosen application        
        #TMyTestForm, MyTestForm
        self.topHwnd=launchApp(APP,wantedText='MyTestForm')
            
    def tearDown(self):
        # close the chosen application                    
        terminateApp(self.topHwnd)
               
    def test_init(self):
        # startup form
        # look for "MyInitValue" in text box MyEditBox
        hwnd=findControl(self.topHwnd,wantedText='MyInitValue')
        t=getEditText(hwnd)
        self.assertEquals(t,['MyInitValue'])  
        # look for ComboBox
        p=PerformanceCheck()
        combo_hwnd=findControl(self.topHwnd,wantedClass='Edit') # it's in the Edit element
        t=getEditText(combo_hwnd)
        self.assertEquals(t,['Select a fruit ..'])
        p.check('select a fruit')
        
        # check the status of the menus
        #hm=getTopMenu(self.topHwnd)
        #print win32gui.GetMenuState(hm,)
                      
    def test_button1(self):
        # click button1 and look for "hello" in text box MyEditBox
        hwbutton=findControl(self.topHwnd,wantedClass='TButton',wantedText='Button1')
        
        hwnd=findControl(self.topHwnd,wantedText='MyInitValue')
       
        # wrap these controls in a timecheck!
        t=PerformanceCheck()
        clickButton(hwbutton)
        self.assertEquals(getEditText(hwnd),['hello'])
        t.check('Button1 being pressed',1.0)
             
    def test_button2(self):
        # click button2 and look for "Not" in text box MyEditBox
        hwnd=findControl(self.topHwnd,wantedText='Button2')
        clickButton(hwnd)
        hwnd=findControl(self.topHwnd,wantedText='MyInitValue')
        self.assertEquals(getEditText(hwnd),['Not']) 
              
class ExampleCase2Again(TestCase):
    def setUp(self): 
        # launch the chosen application        
        #TMyTestForm, MyTestForm
        self.pwin=PLaunchApp(APP,wantedText='MyTestForm')
         
    def tearDown(self):
        # close the chosen application
        #ensure we have the right window for ensuring exit         
        
        self.pwin.terminate()
         
    def test_init(self):
        # startup form
        # look for "MyInitValue" in text box MyEditBox
        edbox=self.pwin.findPControl(wantedClass='TEdit')
        self.assertEquals(edbox.text,'MyInitValue')  
        # look for ComboBox
        combo_box=self.pwin.findPControl(wantedClass='TComboBox') 
        self.assertEquals(combo_box.text,['Select a fruit ..'])
                              
    def xtest_button1(self):
        # click button1 and look for "hello" in text box MyEditBox
        button=self.pwin.findPControl(wantedClass='TButton',wantedText='Button1')
        button.click()
        edbox=self.pwin.findPControl(wantedClass='TEdit',wantedText='MyInitValue')
        p=PerformanceCheck()
        button.click()
        self.assertEquals(edbox.text,'hello') 
        p.check('Pcontrol Button check',2.0)
              
    def test_button2(self):
        # click button2 and look for "Not" in text box MyEditBox
        b2=self.pwin.findPControl(wantedText='Button2')
        b2.click()
        edbox=self.pwin.findPControl(wantedClass='TEdit',wantedText='MyInitValue')
        self.assertEquals(edbox.text,'Not') 
                            
    def test_menu1(self):
       
        editbox=self.pwin.findPControl(wantedText='MyInitValue')
        combobox=self.pwin.findPControl(wantedClass='TComboBox')        
        
        m=MenuItem(self.pwin,('File','&Open'))

        p=PerformanceCheck()
        m.activate()
        self.assertEquals(editbox.text,'kikk') 
        self.assertEquals(combobox.text,['junk']) 
        p.check('Clicking the menu',0.5)
        
    def test_menu2(self):
       
        editbox=self.pwin.findPControl(wantedText='MyInitValue')
        combobox=self.pwin.findPControl(wantedClass='TComboBox')        
        
        m=MenuItem(self.pwin,'File','&Open') #accepts *args-type
        m.activate()
       
        self.assertEquals(editbox.text,'kikk') 
        self.assertEquals(combobox.text,['junk'])     
    
    def test_listbox(self):
        listbox=self.pwin.findPControl(wantedClass='TListBox')
        self.assertEquals(listbox.items, ['Cricket', 'Football', 'Golf'])
    
    def test_combobox(self):
        combobox=self.pwin.findPControl(wantedClass='TComboBox')
        self.assertEquals(combobox.items, ['Pineapples', 'Apple', 'Orange'])
         
    def test_checkbox(self):
        checkbox=self.pwin.findPControl(wantedClass='TCheckBox')
        #self.assertEquals(checkbox.text,'CheckBox1')
         
    def test_memo(self):
        memo=self.pwin.findPControl(wantedClass='TMemo')
        #self.assertEquals(memo.items, ['Mem'])

 
##if __name__=='__main__':
##    unittest.main()
